﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace RENIT.DragNDropUpload.Utils
{
    public class WebGuard : IDisposable
    {
        private SPSite site;
        private bool siteOwned;
        private SPWeb web;
        private bool webOwned;


        private WebGuard(SPWeb targetWeb, bool siteIsOwned, bool webIsOwned)
        {
            site = targetWeb.Site;
            siteOwned = siteIsOwned;
            web = targetWeb;
            webOwned = webIsOwned;
        }

        private WebGuard(Guid siteID, Guid webID)
        {
            site = new SPSite(siteID);
            siteOwned = true;
            web = site.OpenWeb(webID);
            webOwned = true;
        }

        public static WebGuard GetCurrent()
        {
            return new WebGuard(SPContext.Current.Web, false, false);
        }

        public DocumentLibraryAdapter GetDocumentLibraryByFolder(string webRelativeFolderUrl)
        {
            return new DocumentLibraryAdapter(this, webRelativeFolderUrl);
        }

        public DocumentLibraryAdapter GetDocumentLibraryByListId(Guid id)
        {
            return new DocumentLibraryAdapter(this, id);
        }

        public bool Disposed { get; private set; }

        #region IDisposable Members

        public void Dispose()
        {
            if (Disposed)
            {
                return;
            }
            if (webOwned)
            {
                web.Dispose();
            }
            web = null;
            if (siteOwned)
            {
                site.Dispose();
            }
            site = null;
            Disposed = true;
        }

        #endregion

        internal SPFolder GetFolder(string webRelativeFolderUrl)
        {
            return web.GetFolder(webRelativeFolderUrl);
        }

        internal SPList GetList(Guid guid)
        {
            return web.Lists[guid];
        }

        internal SPList GetList(string idOrTitle)
        {
            SPList list = null;
            try
            {
                Guid id = new Guid(idOrTitle);
                list = web.Lists[id];
            }
            catch
            {
                list = web.Lists[idOrTitle];
            }
            return list;
        }


        internal static WebGuard OpenWebOfCurrentSite(Guid guid)
        {
            var currentSite = SPContext.Current.Site;
            return new WebGuard(currentSite.OpenWeb(guid), false, true);
        }

        public bool IsDocumentLibrary(string idOrTitle)
        {
            return GetList(idOrTitle).BaseType == SPBaseType.DocumentLibrary;
        }


        public static WebGuard OpenWeb(Guid siteID, Guid webID)
        {
            return new WebGuard(siteID, webID);
        }

        public IDisposable AllowUsafeUpdates()
        {
            bool oldSiteUnsafeUpdates = site.AllowUnsafeUpdates;
            bool oldWebUnsafeUpdates = web.AllowUnsafeUpdates;
            site.AllowUnsafeUpdates = true;
            web.AllowUnsafeUpdates = true;
            return new DisposingLock(() =>
                {
                    site.AllowUnsafeUpdates = oldSiteUnsafeUpdates;
                    web.AllowUnsafeUpdates = oldWebUnsafeUpdates;
                });
        }

        public bool CurrentUserCanUploadFilesInto(string idOrTitle)
        {
            SPList list = GetList(idOrTitle);
            return list.DoesUserHavePermissions(SPBasePermissions.AddListItems);
        }
    }
}
